package cz.semjob.learning.data.attribute;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cz.semjob.common.attribute.IDistributionAttribute;

public class TextDistributionInstanceAttribute extends AInstanceAttribute
		implements IDistributionAttribute {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3284116789547902130L;

	private int textCount;

	private Map<String, List<String>> documentTextMap;
	private Map<String, Integer> textDistribution;

	public TextDistributionInstanceAttribute() {
		super();
	}

	public TextDistributionInstanceAttribute(String name) {
		super(name);
		textCount = 0;
		textDistribution = new HashMap<String, Integer>();
		documentTextMap = new HashMap<String, List<String>>();
	}

	// for each document we count text only once
	public void addTextToDistribution(String documentId, String text) {
		List<String> documentTexts = null;
		if (documentTextMap.containsKey(documentId)) {
			documentTexts = documentTextMap.get(documentId);
			if (documentTexts.contains(text)) {
				// do nothing
				return;
			}
		} else {
			documentTexts = new ArrayList<String>();
			documentTextMap.put(documentId, documentTexts);

		}
		documentTexts.add(text);
		textCount++;
		if (textDistribution.containsKey(text)) {
			textDistribution.put(text, textDistribution.get(text) + 1);
		} else {
			textDistribution.put(text, 1);
		}

	}

	public Double getDistributionForText(String text) {
		if (textDistribution.containsKey(text)) {
			return ((double) textDistribution.get(text)) / ((double) textCount);
		} else {
			return 0d;
		}

	}

}
